#include "game.h"
#include <hgefont.h>
#include "GameManager.h"

BaseGame *g_pBaseGame = NULL;
HGE *g_pHge = NULL;
//

bool BaseGame::FrameFunc()
{ 
	if(g_pHge)
	{
		return !g_pBaseGame->Update(g_pHge->Timer_GetDelta()); 
	}
	else 
		return !false;
}

bool BaseGame::RenderFunc() 
{ 
	return !g_pBaseGame->Render(); 
}


bool BaseGame::GfxRestoreFunc()
{	
		return g_pBaseGame->Restore();
}
BaseGame::BaseGame()
{
	SetClassID(kCLASSID_BASEGAME);
	g_pBaseGame = this;
	m_pGameMgr = NULL;
}

BaseGame::~BaseGame()
{
	BaseGame::Cleanup();
	g_pBaseGame = NULL;
}

void BaseGame::Cleanup()
{
	if(m_pHge)
	{
		// Restore video mode and free all allocated resources
		m_pHge->System_Shutdown();

		// Release the HGE interface. If there are no more references,
		// the HGE object will be deleted.
		m_pHge->Release();
		g_pHge = m_pHge = NULL;
	}
}

bool BaseGame::Start()
{
	// Here we use global pointer to HGE interface. Instead you may use hgeCreate() every
	// time you need access to HGE. Just be sure to have a corresponding m_pHge->Release()
	// for each call to hgeCreate()
	g_pHge = m_pHge = hgeCreate(HGE_VERSION);
	if(!m_pHge)
	{
		MessageBox(NULL, L"BaseGame - Failed to start HGE", L"Error", MB_OK | MB_ICONERROR | MB_APPLMODAL);
		return false;
	}

	m_pHge->System_SetState(HGE_LOGFILE, "hgeGame.log");
	//m_pHge->System_SetState(HGE_INIFILE, "hgeGame.ini");
	m_pHge->System_SetState(HGE_FRAMEFUNC, &Game::FrameFunc);
	m_pHge->System_SetState(HGE_RENDERFUNC, &Game::RenderFunc);
	m_pHge->System_SetState(HGE_WINDOWED, false);
	m_pHge->System_SetState(HGE_SCREENWIDTH, hSCREEN_WIDTH);
	m_pHge->System_SetState(HGE_SCREENHEIGHT, hSCREEN_HEIGHT);
	m_pHge->System_SetState(HGE_SCREENBPP, 32);
	m_pHge->System_SetState(HGE_USESOUND, true);
	m_pHge->System_SetState(HGE_SHOWSPLASH, false);
	m_pHge->System_SetState(HGE_HIDEMOUSE, true);
	m_pHge->System_SetState(HGE_TITLE, "XploreMas - imba ed.");
	//m_pHge->System_SetState(HGE_GFXRESTOREFUNC, &Game::GfxRestoreFunc);
	//m_pHge->System_SetState(HGE_FPS, 50 );


	// Tries to initiate HGE with the states set. If something goes wrong, "false" is returned
	// and more specific description of what have happened can be read with System_GetErrorMessage().
	if( !m_pHge->System_Initiate())
	{
		// If HGE initialization failed show error message
		MessageBox(NULL, LPCWSTR(m_pHge->System_GetErrorMessage()), L"Error", MB_OK | MB_ICONERROR | MB_APPLMODAL);
		return false;
	}	

	// initialize more here
	if( !Initialize())
	{
		MessageBox(NULL, L"BaseGame - Initilize has failed", L"Error", MB_OK | MB_ICONERROR | MB_APPLMODAL);
		m_pHge->System_Log("BaseGame - Initilization failed");
		
		return false;
	}

	m_pGameMgr = new GameManager(m_pDebugFont);

	// Starts running FrameFunc(). Note that the execution "stops" here
	// until "true" is returned from FrameFunc().
	m_pHge->System_Start();

	return true;
}

bool BaseGame::Initialize()
{
	m_pHge->System_Log("Game - Initialize");

	// Load debug font
	m_pHge->System_Log("Game - Loading debug font");
	m_pDebugFont = new hgeFont("content\\fonts\\consolas.fnt");
	if(!m_pDebugFont)
		m_pHge->System_Log("Game - Loading of debug font has failed, font missing?");
	else
	{
		m_pDebugFont->SetScale(1);
		m_pDebugFont->SetColor(ARGB(255,200,200,200) );
	}
	return true;
}

bool BaseGame::Update(float fDeltaTime)
{
	switch(m_pHge->Input_GetKey())
	{
		case HGEK_ESCAPE:
			return false;
		break;
	}

	
	return m_pGameMgr->Update( fDeltaTime );
}


bool BaseGame::Render()
{
	////Render current Scene to Texture and transforms the cameracoords.
	//m_pHge->Gfx_BeginScene( m_pGameMgr->GetTarget() );
	//m_pHge->Gfx_Clear(ARGB(255,255,255,255));
	////transform the camera coords
	//
	//m_pGameMgr->Render();
	//m_pHge->Gfx_EndScene();

	//m_pHge->Gfx_SetTransform();
	//Render Texture
	m_pHge->Gfx_BeginScene();
	m_pHge->Gfx_Clear(ARGB(255,0,0,0));
	m_pGameMgr->Render();
	m_pHge->Gfx_SetTransform();
	//m_pGameMgr->GetTargetSprite()->Render(0,0);
	//m_pDebugFont->printf(5,5,HGETEXT_LEFT,"FPS: %d", m_pHge->Timer_GetFPS() );
	m_pHge->Gfx_EndScene();
	

	return true;
}

bool BaseGame::Restore()
{
	return m_pGameMgr->RestoreFunc();

}